package com.kfm.pm.poker;

import java.util.*;

public class GameRoom {

    private List playerList1 = new ArrayList<>(); // 存储第一个人的卡牌列表
    private List playerList2 = new ArrayList<>(); // 存储第二个人的卡牌列表
    private List playerList3 = new ArrayList<>(); // 存储第三个人的卡牌列表
    private List bottomList = new ArrayList();
    private List cardList = new ArrayList();
    public GameRoom() {
        String[] colors = {"♣", "♦", "♥", "♠"};
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        // 除了大小王
        for (int i = 0; i < 4; i ++) {
            String color = colors[i];
            for (int j = 0; j < 13; j++ ) {
                Card card = new Card(color, numbers[j], j + 1);
                cardList.add(card);
            }
        }
        Card card1 = new Card("", "🤡", 50);
        Card card = new Card("", "🃏", 100);
        Collections.addAll(cardList, card, card1);

    }

    public void start() {
        System.out.println("========================");
        System.out.println("欢迎来到开发喵斗地主游戏房间");
        System.out.println("1. 洗牌");
        System.out.println("2. 发牌");
        System.out.println("3. 捋牌");
        System.out.println("4. 看牌");
        System.out.println("5. 抢地主");
        System.out.println("0. 退出");
        System.out.println("========================");
        Scanner sc = new Scanner(System.in);
        int op = sc.nextInt();
        switch (op) {
            case 1 -> washPoker();
            case 2 -> deal();
            case 3 -> sortPoker();
            case 4 -> watchPoker();
            case 5 -> System.out.println("等待开发");
            case 0 -> System.exit(1);
        }
        start();
    }

    private void washPoker() {
        Collections.shuffle(cardList);
        System.out.println("洗牌完成");
    }
    /**
     * 开始发牌
     */
    private void deal() {
//        for (int i = 0; i < 17; i ++) {
//            playerList1.add(cardList.get(i)); // 玩家一卡牌列表
//        }
//        cardList.removeAll(playerList1);
//        for (int i = 0; i < 17; i ++) {
//            playerList2.add(cardList.get(i));
//        }
//        cardList.removeAll(playerList2);
//        for (int i = 0; i < 17; i ++) {
//            playerList3.add(cardList.get(i));
//        }
//        cardList.removeAll(playerList3);
//        bottomList.addAll(cardList);

        //
//        Iterator iterator = cardList.iterator();
//        while (iterator.hasNext()) {
//            for (int i = 0; i < 17; i ++) {
//                playerList1.add(iterator.next());
//            }
////            for () {
////
////            }
//        }
        int key = 0;
        int size = cardList.size() - 3;
        // 1 2 3 4 5 6
        for (int i = 0; i < size / 3; i ++) {
            playerList1.add(cardList.get(key));
            cardList.remove(key);
            playerList2.add(cardList.get(key));
            cardList.remove(key);
            playerList3.add(cardList.get(key));
            cardList.remove(key);
        }
        System.out.println("发牌完成");
        // 0 - 17   17 - 35  35 - 52
//        playerList1.addAll(cardList.subList(0, 17)); // [0, 16]
//        playerList2.addAll(cardList.subList(17, 34)); // [17, 33]
//        playerList1.addAll(cardList.subList(34, 51)); // [34, 50]
//        bottomList.addAll(cardList.subList(51, 54)); // [51, 53]
    }

    // 捋牌
    private void sortPoker() {
        // 比较器
        Comparator c = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Card card = (Card) o1;
                Card card2 = (Card) o2;
                return card.getSize() - card2.getSize();
            }
        };
        // 玩家一
        playerList1.sort(c);
        playerList2.sort(c);
        playerList3.sort(c);
    }

    // 看牌
    private void watchPoker() {
        forEach(playerList1);
        forEach(playerList2);
        forEach(playerList3);
        forEach(bottomList);
    }

    private void forEach(List list) {
        list.forEach(obj -> System.out.print(obj + " "));
        System.out.println();
    }
}
